package com.example.databasetest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;

public class MainActivity extends AppCompatActivity {

    private MyDatabaseHelper dbHelper;

    //private MyDatabaseHelper dbHelper1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SQLiteStudioService.instance().start(this);

        dbHelper = new MyDatabaseHelper(this, "StudentStore.db", null, 1);
        //dbHelper1=new MyDatabaseHelper(this,"Book.db",null,1);

        Button creatDatabase = (Button) findViewById(R.id.creat_database);
        creatDatabase.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
                //dbHelper1.getWritableDatabase();
            }
        });

        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", "Zhangsan");
                values.put("number", "20200513");
                db.insert("Student", null, values);
                values.clear();
                values.put("name", "Lisi");
                values.put("number", "20200514");
                db.insert("Student", null, values);
            }
        });

        Button queryButton = (Button) findViewById(R.id.query_data);
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("Student", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {
                    do {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String number = cursor.getString(cursor.getColumnIndex("number"));
                        Log.d("MainAvtivity", "name: " + name + ",number:" + number);

                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        });

    }
}
